#pragma once
#include "../Comm/Comm.h"
#include "../ObjectPool/ObjectPool.h"
#include "PageMap.h"


//PageCache同样设计为单例的
class PageCache
{
public:
    static PageCache* GetInstance();
    std::mutex& GetMutex();
    Span* NewSpan(size_t npages);
    Span* MapObjectToSpan(void* obj);
    void ReleaseToPage(Span* span);            

    
private:
    PageCache();
    ~PageCache() = default;
    PageCache(const PageCache&) = delete;
    PageCache& operator=(const PageCache&) = delete;
private:
    SpanList _spanLists[NPAGES];
    // std::unordered_map<PAGE_TYPE,Span*> _idSpanMap;//通过PAGE_ID来辨别是哪个span的哈希结构
    #ifdef __x86_64__
    //64位版本pageMap:自身根据原理实现
    TCMalloc_PageMap3<64 - PAGE_SHIFT> _idSpanMap;
    #else
    TCMalloc_PageMap1<32 - PAGE_SHIFT> _idSpanMap;
    // ObjectPool<Span> _pool;//自身定长内存池用于为Span动态开辟空间
    #endif

    std::mutex _mtx;
    static PageCache _sIns;//饿汉模式
};

